using System;
using Fornax020.SerialComm.Core;
using Fornax020.SerialComm.Protocol;

namespace Fornax020.SerialComm.Commands
{
    /// <summary>
    /// MA设置命令
    /// </summary>
    public class SetMaCommand : CommandBase
    {
        private readonly double _maValue;

        public SetMaCommand(double maValue)
        {
            _maValue = maValue;
            Name = "SetMA";
            Description = $"设置MA值为 {maValue}";
        }

        public override byte[] GetCommandData()
        {
            // 根据协议生成CP设置命令
            // 格式: STX + 'C' + 'P' + 数值 + CR
            byte[] command = new byte[8];
            command[0] = (byte)ProtocolConstants.STX;  // 起始字节
            command[1] = (byte)'C';  // CP指令
            command[2] = (byte)'P';  // CP指令
            
            // 将MA值转换为4位数字字符串
            int maRaw = (int)_maValue; // MA值通常为整数
            string maString = maRaw.ToString("D4");
            
            // 填充MA值
            for (int i = 0; i < 4; i++)
            {
                command[3 + i] = (byte)maString[i];
            }
            
            command[7] = (byte)ProtocolConstants.CR;   // 结束字节
            
            return command;
        }
    }
}
